﻿/* 2012/5/3 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;

namespace PictureShower
{
    [RunInstaller(true)]
    public partial class PictureShowerInstaller : Installer
    {
        public PictureShowerInstaller()
        {
            InitializeComponent();
        }

        private string GetUserDesktopPath()
        {
            Microsoft.Win32.RegistryKey rootRegistryKey = Microsoft.Win32.Registry.CurrentUser;
            return rootRegistryKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders").GetValue("Desktop").ToString();
        }

        private void CreateShortcut(string shortcutPath, string targetPath, string description)
        {
            if (!Path.GetExtension(shortcutPath).ToLower().Equals(".lnk"))
                Path.ChangeExtension(shortcutPath, ".lnk");

            if (File.Exists(shortcutPath))
                return;

            IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
            IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath);
            shortcut.TargetPath = targetPath;
            shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);
            shortcut.WindowStyle = 1;
            shortcut.Description = description;
            shortcut.Save();
        }

        private void PictureShowerInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            string shortcutPath = Path.Combine(GetUserDesktopPath(), "Picture Shower.lnk");
            string targetPath = Path.ChangeExtension(System.Reflection.Assembly.GetExecutingAssembly().Location, ".scr");
            File.Copy(System.Reflection.Assembly.GetExecutingAssembly().Location, targetPath, true);
            string shortcutDescription = "Picture Shower 屏幕保护程序";
            CreateShortcut(shortcutPath, targetPath, shortcutDescription);
        }

        private void PictureShowerInstaller_AfterUninstall(object sender, InstallEventArgs e)
        {
            string shortcutPath = Path.Combine(GetUserDesktopPath(), "Picture Shower.lnk");
            if (File.Exists(shortcutPath))
                File.Delete(shortcutPath);
        }
    }
}
